package main.java.middle;

import main.java.structure.ListNode;

/**
 * @Author: 郑国荣
 * @Date: 2023-08-15-9:33
 * @Description:
 */
public class GetSum {
    public static ListNode getSum(ListNode l1, ListNode l2){
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        int carray = 0;
        while (l1!=null || l2!=null || carray>0){
            int sum = carray;
            if (l1!=null){
                sum+=l1.val;
                l1 = l1.next;
            }
            if (l2!=null){
                sum+=l2.val;
                l2 = l2.next;
            }
            carray = sum/10;
            sum %= 10;
            current.next = new ListNode(sum);
            current = current.next;
        }

        return  dummy.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(2);
        ListNode second = new ListNode(2);
        ListNode third = new ListNode(3);

        head.next = second;
        second.next = third;

        ListNode head2 = new ListNode(9);
        ListNode second2 = new ListNode(2);
        ListNode third2 = new ListNode(3);
        ListNode forth = new ListNode(3);

        head2.next = second2;
        second2.next = third2;
        third2.next = forth;

        ListNode head0 = getSum(head,head2);
        printList(head0);

    }



    public static void printList(ListNode l){
        ListNode current = l;
        while (current!=null){
            System.out.print(current.val);
            current = current.next;
        }
    }

}
